/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : GPIO LED Example
 ******************************************************************************
 * @attention
 *
 * GPIO和LED控制示例
 * 功能：
 *   - GPIO初始化
 *   - LED点亮/熄灭
 *   - LED闪烁
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx.h"
#include "bsp_gpio.h"

/* Private defines -----------------------------------------------------------*/
#define LED1_PORT       GPIOB
#define LED1_PIN        GPIO_PIN_0

#define LED2_PORT       GPIOB
#define LED2_PIN        GPIO_PIN_1

#define LED3_PORT       GPIOB
#define LED3_PIN        GPIO_PIN_2

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void led_init(void);
void led_on(GPIO_TypeDef *port, uint16_t pin);
void led_off(GPIO_TypeDef *port, uint16_t pin);
void led_toggle(GPIO_TypeDef *port, uint16_t pin);

/* Private functions ---------------------------------------------------------*/

/**
 * @brief       LED初始化
 *
 * @param       无
 *
 * @retval      无
 */
void led_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* 使能GPIO时钟 */
    __HAL_RCC_GPIOB_CLK_ENABLE();

    /* 配置LED引脚 */
    GPIO_InitStruct.Pin = LED1_PIN | LED2_PIN | LED3_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(LED1_PORT, &GPIO_InitStruct);

    /* 初始状态：LED熄灭 */
    HAL_GPIO_WritePin(LED1_PORT, LED1_PIN, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(LED2_PORT, LED2_PIN, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(LED3_PORT, LED3_PIN, GPIO_PIN_RESET);
}

/**
 * @brief       LED点亮
 *
 * @param[in]   port - GPIO端口
 * @param[in]   pin - GPIO引脚
 *
 * @retval      无
 */
void led_on(GPIO_TypeDef *port, uint16_t pin)
{
    HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET);
}

/**
 * @brief       LED熄灭
 *
 * @param[in]   port - GPIO端口
 * @param[in]   pin - GPIO引脚
 *
 * @retval      无
 */
void led_off(GPIO_TypeDef *port, uint16_t pin)
{
    HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET);
}

/**
 * @brief       LED翻转
 *
 * @param[in]   port - GPIO端口
 * @param[in]   pin - GPIO引脚
 *
 * @retval      无
 */
void led_toggle(GPIO_TypeDef *port, uint16_t pin)
{
    HAL_GPIO_TogglePin(port, pin);
}

/**
 * @brief       系统时钟配置
 *
 * @param       无
 *
 * @retval      无
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /* 配置HSE */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 5;
    RCC_OscInitStruct.PLL.PLLN = 160;
    RCC_OscInitStruct.PLL.PLLP = 2;
    RCC_OscInitStruct.PLL.PLLQ = 4;
    RCC_OscInitStruct.PLL.PLLR = 2;
    RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL_RANGE_VCO_250_500M;
    RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL_VCORANGE_WIDE;
    RCC_OscInitStruct.PLL.PLLFRACN = 0;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    /* 配置时钟分频 */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
}

/**
 * @brief       主函数
 *
 * @param       无
 *
 * @retval      int
 */
int main(void)
{
    /* 初始化 */
    HAL_Init();
    SystemClock_Config();
    led_init();

    /* 主循环 */
    while(1)
    {
        /* 示例1：LED闪烁 */
        led_toggle(LED1_PORT, LED1_PIN);
        HAL_Delay(500);

        /* 示例2：LED流水灯 */
        led_on(LED2_PORT, LED2_PIN);
        HAL_Delay(200);
        led_off(LED2_PORT, LED2_PIN);

        led_on(LED3_PORT, LED3_PIN);
        HAL_Delay(200);
        led_off(LED3_PORT, LED3_PIN);
    }

    return 0;
}

/**
 * @brief       系统异常处理
 *
 * @param       无
 *
 * @retval      无
 */
void HardFault_Handler(void)
{
    while(1)
    {
        led_toggle(LED1_PORT, LED1_PIN);
        HAL_Delay(100);
    }
}
